package com.fira.api.controller;

import com.fira.api.domain.dto.EduStudentDTO;
import com.fira.api.domain.dto.LoginDTO;
import com.fira.api.domain.dto.StuRegistrationDTO;
import com.fira.api.service.IStudentService;
import com.fira.common.annotation.RepeatSubmit;
import com.fira.common.core.domain.AjaxResult;
import com.fira.common.utils.StringUtils;
import com.fira.edu.domain.EduStudent;
import com.fira.edu.domain.dto.StudentCourseListDTO;
import com.fira.edu.domain.vo.EduStudentVO;
import com.fira.edu.service.IEduStudentCourseService;
import com.fira.edu.service.IEduStudentService;
import com.fira.edu.service.IEduStudentSettleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 学生服务控制器
 *
 * @author henryGui
 * @version V1.0
 * @Title: StuRegistrationController
 * @Package com.fira.api.controller
 * @Description: TODO
 * @date 2021-10-11 下午10:03
 */

@Controller
@RequestMapping("/api/stu")
public class StudentController extends ApiController {

    @Autowired
    private IStudentService iStudentService;
    @Autowired
    private IEduStudentService iEduStudentService;
    @Autowired
    private IEduStudentSettleService eduStudentSettleService;
    @Autowired
    private IEduStudentCourseService eduStudentCourseService;

    @ApiOperation(value = "新生报名", tags = {"新生报名"}, notes = "")
    @PostMapping("/stuRegistration")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult stuRegistration(@RequestBody @Valid StuRegistrationDTO registrationDTO) {
        LoginDTO dto = getLogin(getRequest());
        registrationDTO.getEduStudent().setTenantId(dto.getCurrentTenantId());
        registrationDTO.getEduStudentSettle().setAgentPersonId(dto.getTeacherId());
        return iStudentService.saveRegistrationOrder(registrationDTO);
    }

    @ApiOperation(value = "查询学生列表", tags = {"查询学生列表"}, notes = "")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult getStudentList(HttpServletRequest request, EduStudentDTO eduStudentDTO) {
        LoginDTO dto = getLogin(request);
        eduStudentDTO.setTenantId(dto.getCurrentTenantId());
        return iStudentService.selectEduStudentList(eduStudentDTO);
    }

    @ApiOperation(value = "查询学生明细", tags = {"查询学生明细"}, notes = "")
    @PostMapping("/selectStudentInfoById")
    @ResponseBody
    public AjaxResult selectStudentInfoById(String studentId) {
        return iStudentService.selectStudentInfoById(studentId);
    }

    @ApiOperation(value = "查询学生报读课程(包括在读和历史)", tags = {"查询学生报读课程(包括在读和历史)"}, notes = "")
    @PostMapping("/selectStudentCourseById")
    @ResponseBody
    public AjaxResult selectStudentCourseById(String studentId) {
        return iStudentService.selectStudentCourseById(studentId);
    }

    @ApiOperation(value = "查询学生历史报读课程（分页）", tags = {"查询学生历史报读课程（分页）"}, notes = "")
    @PostMapping("/pageHistoryCourseById")
    @ResponseBody
    public AjaxResult pageHistoryCourseById(String studentId) {
        startPage();
        List list = iStudentService.selectHistoryCourseById(studentId);
        return AjaxResult.success("查询成功", list);
    }

    @ApiOperation(value = "查询学生在读报读课程", tags = {"查询学生在读报读课程"}, notes = "")
    @PostMapping("/selectOnlineCourseById")
    @ResponseBody
    public AjaxResult selectOnlineCourseById(String studentId) {
        List list = iStudentService.selectOnlineCourseById(studentId);
        return AjaxResult.success("查询成功", list);
    }

    @ApiOperation(value = "查询学生订单信息（即结算信息）", tags = {"查询学生报读课程(包括在读和历史)"}, notes = "")
    @PostMapping("/selectStudentOrderById")
    @ResponseBody
    public AjaxResult selectStudentOrderById(@RequestBody String studentId) {
        return iStudentService.selectStudentOrderById(studentId);
    }

    @PostMapping("/historyStuList")
    @ApiOperation(value = "查询历史学生信息（分页）", tags = {"查询历史学生信息（分页）"}, notes = "")
    @ResponseBody
    public AjaxResult historyList(EduStudent eduStudent) {
        startPage();
        LoginDTO loginDTO = getLogin(getRequest());
        eduStudent.setTenantId(loginDTO.getCurrentTenantId());
        List<EduStudentVO> list = iEduStudentService.selectStudentHistoryList(eduStudent);
        return AjaxResult.success(getDataTable(list));
    }

    @PostMapping("/onlineStuList")
    @ApiOperation(value = "查询在读学生信息（不分页）", tags = {"查询在读学生信息（不分页）"}, notes = "")
    @ResponseBody
    public AjaxResult onlineList(EduStudent eduStudent) {
        LoginDTO loginDTO = getLogin(getRequest());
        eduStudent.setTenantId(loginDTO.getCurrentTenantId());
        List<EduStudentVO> list = iEduStudentService.selectStudentOnlineList(eduStudent);
        return AjaxResult.success(getDataTable(list));
    }

    @PostMapping("/stuOrders")
    @ApiOperation(value = "查询学生订单列表", tags = {"查询学生订单列表"}, notes = "")
    @ResponseBody
    public AjaxResult stuOrders(@RequestParam String studentId){
        if(StringUtils.isEmpty(studentId)){
            return AjaxResult.error("学生ID不能为空");
        }
        return AjaxResult.success("查询成功",eduStudentSettleService.selectStudentOrders(studentId));
    }

    @PostMapping("/stuCourseSchedule")
    @ApiOperation(value = "查询学生课表列表（包含已上课表和周课表）", tags = {"查询学生课表列表（包含已上课表和周课表）"}, notes = "")
    @ResponseBody
    public AjaxResult getStudentCourseSchedule(@ApiParam(name = "学生课表列表查询入参") @Valid @RequestBody StudentCourseListDTO studentCourseListDTO){
        return AjaxResult.success("查询成功",eduStudentCourseService.selectStudentCourseInfoList(studentCourseListDTO));
    }

    @PostMapping("/stuParents")
    @ApiOperation(value = "查询学生家长列表", tags = {"查询学生家长列表"}, notes = "")
    @ResponseBody
    public AjaxResult getStudentParents(@ApiParam(name = "学生ID") @RequestParam String studentId){
        if(StringUtils.isEmpty(studentId)){
            return AjaxResult.error("学生ID不能为空");
        }
        return AjaxResult.success("查询成功",iEduStudentService.selectStudentParents(studentId));
    }
}
